<?php

$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl
TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/
Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB
AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH
2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6
ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL
3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq
deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+
qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f
oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2
4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL
QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY
yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c
-----END RSA PRIVATE KEY-----';
//自己生成的私钥

$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3//sR2tXw0wrC2DySx8vNGlqt
3Y7ldU9+LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl
Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o
2n1vP1D+tD3amHsK7QIDAQAB
-----END PUBLIC KEY-----';
//自己生成的公钥

$public_bopay_key='-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOAoslcPOFmqk/Okv5sT3z+Tsn
wjCXtev4OPTM9oLQpr7DwHNYlXIxGkI0rf0RWW6zKMXvrNCYXBjanUYvi0ukM0uj
LJiZ+qMutRzxkckqN1ZXSRsjPoCG7S46M1Ew52TKYYkPm/53gqe+gQzdIEDAg8cu
xIbSiuKGr2em/jnRfQIDAQAB
-----END PUBLIC KEY-----';
/*由BOPAY提供的公钥*/

    $businessHead = array(
        'charset' =>'00',
        'version'=>'V1.0',
        'merchantNumber'=>'BOPAY007001007501',
        'tradeType'=>'qqPayApi',
        'requestTime'=>date("Ymdhms"),
        'signType'=>'RSA',
        'sign'=>'',
     );

    $businessContext = array(
        "amount"=> "10",
        "currency"=> "CNY",
        "payType"=> "QQ_NATIVE",
        "orderNumber"=> date("Ymdhms"),
        "commodityDesc"=> "王小二",
        "commodityName"=>"王小二",
        "commodityRemark"=>"王小二",
        "notifyUrl"=> "2",
        "authCode"=> "",
        "orderCreateIp"=> "22.22.2.2",
        "terminalId"=>"11"
        );

        ksort($businessContext);
        $json_businessContext=json_encode($businessContext,JSON_UNESCAPED_UNICODE);/*订单数组转化为JSON格式*/
        $pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的，可用返回资源id Resource id
        //---------------------------------------------------------------------------------------------
        openssl_sign($json_businessContext,$sign,$pi_key,OPENSSL_ALGO_MD5);//根据提供的私钥进行订单签名

        $sign=base64_encode($sign);//最终的签名

        /*echo "<br></br>".$sign;　*/
        /*var_dump($sign);*/
        $businessHead['sign']=$sign;//将签名加入businessHead中

        $arr_order['businessHead']=$businessHead;

        $arr_order['businessContext']=$businessContext;

      /*  组装arr_order格式*/

        ksort($arr_order);

        $json_order=json_encode($arr_order);

        $pu_bopay_key = openssl_pkey_get_public($public_bopay_key);//这个函数可用来判断BOPAY提供的私钥是否是可用的，可用返回资源id Resource id

        foreach (str_split($json_order, 117) as $value) {

            openssl_public_encrypt($value, $encryptDatas,$pu_bopay_key);
           
            $cryptos .= $encryptDatas;
        }

        $context = array(
            'context' => base64_encode($cryptos) ,
            );
        var_dump(json_encode($context));

        list($return_code, $return_content) = http_post_data("http://120.77.174.76:10011/api/pay/qqPay",json_encode($context));//
        echo  "<br><br>".$return_content;

        //返回的密文解密会有
        /*QQ钱包支付请求,成功 decrypt：{"businessContext":{"bopayOrderNumber":"0120171106104256049827005512","orderNumber":"20171106104254","payType":"QQ_NATIVE","amount":10,"currency":"CNY","qrcode":"https://myun.tenpay.com/mqq/pay/qrcode.html?_wv=1027&_bid=2183&t=6V6361f41eacb49950baee8d06f481d4","tradeStatus":"WAIT"},"businessHead":{"merchantNumber":"BOPAY005512005516","sign":"Go7HjWGbyg4CAwBv0Q2jv32qhzBkTx+suWJXGYmPCf/uJYInUsjJWQqCVSFmVB7oZUbZInooJrU8MRUQIt69LWbArOucZ67hYgBe+vJRANRyvaalgKd1reOuwC3KIoVZwABcos5UjF7l96ujm5DSk7MvLwK3j76I9MuCXqnzis8="}}*/
        /*其他的自行处理*/
        exit;  

        function http_post_data($url, $data_string) {  
            $ch = curl_init();  
            curl_setopt($ch, CURLOPT_POST, 1);  
            curl_setopt($ch, CURLOPT_URL, $url);  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
                "Content-Type: application/json; charset=utf-8",  
                "Content-Length: " . strlen($data_string))  
            );  
            ob_start();  
            curl_exec($ch);  
            $return_content = ob_get_contents();  
            ob_end_clean();  
            $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
            return array($return_code, $return_content);  
        } 
   